<template>
	<view class="container">
		<input v-model="userName" class="input" placeholder="请输入用户名"></input>
		<input v-model="firstPassword" password class="input" placeholder="请输入密码"></input>
		<input v-model="secondPassword" password class="input" placeholder="请再次输入密码"></input>
		<button @click="onRegisterButtonClicked()" class="button">注册</button>
	</view>
</template>

<script>
	let tag = 'register';
	export default {
		data() {
			return {
				userName: "",
				firstPassword: "",
				secondPassword: "",
				isLoading: false
			}
		},
		methods: {
			onRegisterButtonClicked() {
				if (this.isLoading) return;

				this.$log.info(tag, "onRegisterButtonClicked");

				if (!this.userName || !this.firstPassword || !this.secondPassword) {
					uni.showToast({
						icon: 'none',
						title: "用户名或密码为空",
						duration: 1500
					});
					return;
				}

				if (this.firstPassword !== this.secondPassword) {
					uni.showToast({
						icon: 'error',
						title: "密码不一致",
						duration: 1500
					});
					this.firstPassword = "";
					this.secondPassword = "";
					return;
				}

				this.isLoading = true;
				this.getCode(this.userName, this.firstPassword);
			},

			getCode(userName, password) {
				this.$log.info(tag, "getCode");
				uni.request({
					url: `${this.constant.BASE_URL}/api/code`,
					method: 'GET',
					data: {
						password: password,
						username: userName
					},
					success: (res) => {
						this.$log.info(tag, "getCode success");
						this.$log.info(tag, JSON.stringify(res.data));
						this.$log.info(tag, res.data.valid_code);
						this.register(userName, password, res.data.valid_code)
					},
					fail: (err) => {
						this.$log.error(tag, "getCode fail: " + err.errMsg);
					}
				});
			},

			register(userName, password, valid_code) {
				this.$log.info(tag, "register");
				uni.request({
					url: `${this.constant.BASE_URL}/api/register`,
					method: 'POST',
					data: {
						password: password,
						username: userName,
						code: valid_code
					},
					success: (res) => {
						this.isLoading = false;
						this.$log.info(tag, "register success");
						this.$log.info(tag, JSON.stringify(res.data));
						if (res.data.code == 200) {
							this.$log.info(tag, userName + "注册成功");
							uni.showToast({
								icon: 'success',
								title: res.data.msg || "注册成功",
								duration: 1500
							});
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/login/login'
								})
							}, 1500);
						} else {
							uni.showToast({
								icon: 'none',
								title: res.data.msg || "注册失败",
								duration: 1500
							});
						}
					},
					fail: (err) => {
						this.isLoading = false;
						this.$log.error(tag, "register fail: " + err.errMsg);
						uni.showToast({
							icon: 'none',
							title: "注册请求失败",
							duration: 1500
						});
					}
				})
			}
		}
	}
</script>

<style>
	.container {
		padding: 20px 20px;
		height: 50vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.input {
		width: 100%;
		border-style: solid;
		border-color: grey;
		border-radius: 20px;
		border-width: 1px;
		padding: 5px 10px;
		font-size: 35rpx;
		margin-bottom: 10px;
	}

	.button {
		width: 100%;
		background-color: #08f;
		font-size: 35rpx;
		color: white;
		font-weight: bold;
		border-radius: 20px;
		margin-bottom: 10px;
	}
</style>